<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * Init Model
 *
 */
class Settings_model extends CI_Model {

    /**
     * User data, stored for embeded methods
     *
     * @access 	private
     * @var 	array
     */
    var $settings = array();

    /**
     * Constructor
     *
     * @uses 	get_settings
     * @return 	void
     */
    function __construct() {
        parent::__construct();
        log_message('debug', 'Init Model Initialized');
        $this->get_settings();
    }

    /**
     * Get Settings
     *
     * Get all the auto loaded settings from the db.
     *
     * @return	array
     */
    function get_settings() {
        if (!$this->db->table_exists('web_settings')) {
            return FALSE;
        }
        $this->db->select('variable,value')->from('web_settings')->where('auto', '1');
        $query = $this->db->get();
        foreach ($query->result() as $k => $row) {
            $this->settings[$row->variable] = $row->value;
        }
        return $this->settings;
    }

    /**
     * Get Setting (Notice Singular)
     *
     * Used to pull out one specific setting from the settings table.
     * */
    function get_setting($name) {
        if (!empty($this->settings[$name])) {
            return $this->settings[$name];
        } else {
            $this->db->select('value')->from('web_settings')->where('variable', $name);
            $query = $this->db->get();
            if ($query->num_rows() > 0) {
                $row = $query->row();
                return $row->value;
            }
            return FALSE;
        }
    }

    function settings_detail() {
        $this->db->select('*');
        $query = $this->db->get('web_settings');
        $result = $query->result();
        return $result;
    }

    function moneyFormat($num) {
        $explrestunits = "";
        if (strlen($num) > 3) {
            $lastthree = substr($num, strlen($num) - 3, strlen($num));
            $restunits = substr($num, 0, strlen($num) - 3); // extracts the last three digits
            $restunits = (strlen($restunits) % 2 == 1) ? "0" . $restunits : $restunits; // explodes the remaining digits in 2's formats, adds a zero in the beginning to maintain the 2's grouping.
            $expunit = str_split($restunits, 2);
            for ($i = 0; $i < sizeof($expunit); $i++) {
                // creates each of the 2's group and adds a comma to the end
                if ($i == 0) {
                    $explrestunits .= (int) $expunit[$i] . ","; // if is first value , convert into integer
                } else {
                    $explrestunits .= $expunit[$i] . ",";
                }
            }
            $thecash = $explrestunits . $lastthree;
        } else {
            $thecash = $num;
        }
        return $thecash; // writes the final format where $currency is the currency symbol.
    }

    //----------- cms models contenent marged in settings model to auto load the cms conent for left menus help--------

    function get_cms() {
        if (!$this->db->table_exists('cms')) {
            return FALSE;
        }
        $this->db->select('*')->from('cms');
        $this->db->where('status', '51');
        $this->db->order_by('id');
        $query = $this->db->get();
        $result = $query->result();
        return $result;
    }

    function get_cms_by_id($cmsID=NULL) {
        $sql = "select * from cms where id=$cmsID";
        $query = $this->db->query($sql);
        $row = $query->row();
        return $row;
    }
    function get_template() {
        if (!$this->db->table_exists('email_template')) {
            return FALSE;
        }
        $this->db->select('*')->from('email_template');
        $this->db->where('status', '51');
        $this->db->order_by('id');
        $query = $this->db->get();
        $result = $query->result();
        return $result;
    }
     function get_template_by_id($id=Null) {
        if ($id) {
            $this->db->select('*')->from('email_template');
            $this->db->where('status', '51');
            $this->db->where('id', $id);
            $query = $this->db->get();
            $result = $query->row();
            return $result;
        }
    }
}